//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_markerDisplay.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if (isDedicated) exitWith {ERROR("Shouldn't be executed on the server.");};
private ["_allUnits","_markers","_units","_unit","_marker","_i","_veh","_x","_name","_vecs","_marker_type","_pos"];
_markers = [];
_units = [];
_vecs = [];
_vecs_markers = [];
while {true} do {
	if (GVAR(AdminCmdMarker) == 1 && GVAR(MarkerMode)) then {
		if (GVAR(ShowVehicleMarkers) == 1) then {
			{
				_veh = _x;
				_name = vehicleVarName _veh;
				if (_name != "" && (_veh isKindOf "AllVehicles")) then {
					TRACE_1("Vehicle Marker",ARR_3(typeof _veh, _veh, _name));
					_marker = format["mrk_%1",_name];
					if (!(_veh in _vecs)) then {
						createMarkerLocal [_marker, getPos _veh];
						_marker setMarkerColorLocal "ColorGreen";
						_marker setMarkerShapeLocal "ICON";
						_marker_type = "mil_box";
						if (_veh isKindOf "Car") then {	_marker_type = "n_mech_inf"; };
						if (_veh isKindOf "Tank") then { _marker_type = "n_armor"; };
						if (_veh isKindOf "Air") then { _marker_type = "n_air"; };
						if (_veh isKindOf "Ship") then { _marker_type = "c_ship"; };
						if (_veh isKindOf "Quadbike_Base_F") then { _marker_type = "n_inf"; };
						if (_veh isKindOf "Offroad_Base") then { _marker_type = "c_car"; };
						_marker setMarkerTypeLocal _marker_type;
						_marker setMarkerSizeLocal [0.7,0.7];
						_marker setMarkerAlphaLocal 0.6;
						_marker setMarkerTextLocal _name;
						_vecs_markers set [count _vecs_markers, _marker];
						_vecs set [count _vecs, _veh];
					};
				};
			} foreach vehicles;
		};
		// Cleaning vehicle markers when needed
		if (count _vecs > 0) then {
			for [{_i = 0},{_i < count _vecs},{_i = _i + 1}] do {
				_veh = _vecs select _i;
				_marker = _vecs_markers select _i;
				if (alive _veh && (GVAR(ShowVehicleMarkers) == 1)) then {
					_marker setMarkerPosLocal (getPos _veh);
					_marker setMarkerAlphaLocal 0.5;
				} else {
					_vecs_markers set [_i, "XxDeLxX"];
					_vecs set [_i, "XxDeLxX"];
					_marker setMarkerPosLocal [0,0,0];
					_marker setMarkerAlphaLocal 0;
					deleteMarkerLocal _marker;
				};
			};
			_vecs = _vecs - ["XxDeLxX"];
			_vecs_markers = _vecs_markers - ["XxDeLxX"];
		};

		_allUnits = switchableUnits;
		if (isMultiplayer) then { _allUnits = playableUnits;};
		// Removing dead units
		if (count _units > 0) then  {
			for [{_i = 0},{_i < count _units},{_i = _i + 1}] do {
				_unit = _units select _i;
				if (isNull _unit) then {
					TRACE_1("Removing player",ARR_4(_unit, isNull _unit, name _unit,_marker));
					_marker = _markers select _i;
					_units set [_i, "XxDeLxX"];
					_marker setMarkerPosLocal [0,0,0];
					_marker setMarkerAlphaLocal 0;
					_markers set [_i, "XxDeLxX"];
					if !(_marker in _markers) then {
						deleteMarkerLocal _marker;
					};
				};
			};
			_units = _units - ["XxDeLxX"];
			_markers = _markers - ["XxDeLxX"];
		};
		// Adding new units
		{
			_unit = _x;
			if (!(_unit in _units)) then {
				_units set[count _units, _unit];
				_marker = format["player_unit_marker_%1",getPlayerUID _unit];
				TRACE_1("Adding player",ARR_4(_unit, isNull _unit, name _unit,_marker));
				_markers set[count _markers, _marker];
				createMarkerLocal [_marker,[0,0,0]];
				_marker setMarkerAlphaLocal 0;
				_marker setMarkerShapeLocal "ICON";
				_marker setMarkerTypeLocal "mil_dot";
				_marker setMarkerColorLocal "ColorBlue";
				_marker setMarkerTextLocal (name _unit);
			};
		} foreach _allUnits;
		if (count _units > 0) then  {
			for [{_i = 0},{_i < count _units},{_i = _i + 1}] do {
				_unit = _units select _i;
				_marker = format["player_unit_marker_%1",getPlayerUID _unit];
				if (alive _unit) then {
					_marker setMarkerAlphaLocal 0.5;
					_pos = (getPos (vehicle _unit));
					_marker setMarkerTextLocal Format["%1",(name _unit),getPlayerUID _unit];
					if (vehicle _unit != _unit) then {
						_marker setMarkerColorLocal "ColorGreen";
						if ((driver (vehicle _unit)) == _unit) then {
							_marker setMarkerColorLocal "ColorKhaki";
						};
						_pos set [1, (_pos select 1) - (((crew (vehicle _unit)) find _unit) * 1.5) - 1.5];
					} else {
						_marker setMarkerColorLocal "ColorBlue";
					};
					_marker setMarkerPosLocal _pos;
				} else {
					_marker setMarkerPosLocal [0,0,0];
					_marker setMarkerAlphaLocal 0;
				};
			};
		};
	} else {
		if (count _markers > 0) then {
			{
				deleteMarkerLocal _x;
			} foreach _markers;
			_markers = [];
		};
		_units = [];
		if (count _vecs_markers > 0) then {
			{
				deleteMarkerLocal _x;
			} foreach _vecs_markers;
			_vecs_markers = [];
		};
		_vecs = [];
	};
	sleep 1.3456;
};

TRACE("End");
TMFPROFILERSTOP;
